﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sebarf.Client;
using Sebarf.Core;
using Sebarf.Core.Interfaces;
using Sebarf.Profiles.WPF;
using Sebarf.Server;
using Sebarf.Services.Impl;
using Sebarf.Services.Interfaces;

#endregion

namespace Sebarf.Tests {
	/// <summary>
	/// Description of the class
	/// </summary>
	[TestClass]
	public class TestProfileRequest {
		#region Public Methods

		[TestMethod]
		[DeploymentItem("ServerProfile.xaml")]
		public void RunTest() {
			IServer server =
				WcfBasedServer.Create(
					delegate {
						return RequestHandlerEngine.Create(new ServiceRegistrationLocatorService(),
															new IRequestHandler[] { new WpfProfileLoadRequestHandler(), new MultiplyRequestHandler() });
					});
			server.Start();

			IClient client = WcfBasedClient.Create("Client 1", null);
			client.Connect();

			client.Process(new WpfBasedProfileLoadRequest { ProfileId = "Profile", ResourceFileName = "ServerProfile.xaml" });
			Assert.AreEqual(Convert.ToInt32(client.Process(new MultiplyRequest { Number1 = 50, Number2 = 80 })), 50 * 80);
			server.Stop();
		}

		#endregion

		#region Private Methods

		// private methods located here

		#endregion

		#region Private Fields

		// private fields located here

		#endregion

		#region Nested type: MultiplyRequest

		[Serializable]
		public class MultiplyRequest : IRequest {
			public double Number1 { get; set; }
			public double Number2 { get; set; }

			public string Id { get; set; }
		}

		#endregion

		#region Nested type: MultiplyRequestHandler

		public class MultiplyRequestHandler : IRequestHandler {
			#region IRequestHandler Members

			public bool CanHandleRequest(IRequest request) {
				return request is MultiplyRequest;
			}

			public object ProcessRequest(IRequest request, IServiceLocatorService serviceLocator) {
				var msg = request as MultiplyRequest;
				return serviceLocator.GetRegisteredService<MultiplierService>().Muliply(msg.Number1, msg.Number2);
			}

			#endregion
		}

		#endregion
	}

	public class MultiplierService : Service {
		public double Muliply(double d1, double d2) {
			return d1 * d2;
		}
	}
}